{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial 0: Before Starting\n",
    "If you want to know about the relevant information and usage instructions about this library, please refer to the README.md or the README.en.md."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Auxiliary parameter generation\n",
    "Currently, we simulate the auxiliary parameters provided by a trusted third party using local files. If you want to generate these auxiliary parameters, please run```./debug/offline_parameter_generation.py```. The auxiliary parameters will be saved as a directory ```data``` by adding the base path .NssMPClib/data/ to the current user's home directory (Linux/Unix: /home/{username}; macOS: /Users/{username}; Windows: C:\\Users\\{username}). \n",
    "\n",
    "Additionally, you can change the number of parameters generated according to your needs. \n",
    "In this way, the parameters can be generated as follows: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-12T09:52:51.329630Z",
     "start_time": "2024-04-12T09:52:44.684356Z"
    }
   },
   "outputs": [],
   "source": [
    "from NssMPC.config import VCMP_SPLIT_LEN\n",
    "from NssMPC.crypto.aux_parameter import *\n",
    "from NssMPC.crypto.aux_parameter.function_secret_sharing_keys.vsigma_key import VSigmaKey\n",
    "from NssMPC.crypto.aux_parameter.select_keys.vos_key import VOSKey\n",
    "from NssMPC.crypto.aux_parameter.truncation_keys.rss_trunc_aux_param import RssTruncAuxParams\n",
    "from NssMPC.crypto.protocols.replicated_secret_sharing.honest_majority_functional.compare import MaliciousCMPKey, MACKey\n",
    "\n",
    "gen_num = 100\n",
    "\n",
    "AssMulTriples.gen_and_save(gen_num, saved_name='2PCBeaver', num_of_party=2, type_of_generation='TTP')\n",
    "AssMulTriples.gen_and_save(gen_num, saved_name='3PCBeaver', num_of_party=3, type_of_generation='TTP')\n",
    "BooleanTriples.gen_and_save(gen_num, num_of_party=2, type_of_generation='TTP')\n",
    "RssMulTriples.gen_and_save(gen_num)\n",
    "\n",
    "GrottoDICFKey.gen_and_save(gen_num)\n",
    "DICFKey.gen_and_save(gen_num)\n",
    "SigmaDICFKey.gen_and_save(gen_num)\n",
    "\n",
    "GeLUKey.gen_and_save(gen_num)\n",
    "TanhKey.gen_and_save(gen_num)\n",
    "ReciprocalSqrtKey.gen_and_save(gen_num)\n",
    "DivKey.gen_and_save(gen_num)\n",
    "\n",
    "Wrap.gen_and_save(gen_num)\n",
    "RssTruncAuxParams.gen_and_save(gen_num)\n",
    "\n",
    "B2AKey.gen_and_save(gen_num)\n",
    "\n",
    "MACKey.gen_and_save(gen_num)\n",
    "\n",
    "VOSKey.gen_and_save(gen_num, 'VOSKey_0')\n",
    "VOSKey.gen_and_save(gen_num, 'VOSKey_1')\n",
    "VOSKey.gen_and_save(gen_num, 'VOSKey_2')\n",
    "\n",
    "VSigmaKey.gen_and_save(gen_num, 'VSigmaKey_0')\n",
    "VSigmaKey.gen_and_save(gen_num, 'VSigmaKey_1')\n",
    "VSigmaKey.gen_and_save(gen_num, 'VSigmaKey_2')\n",
    "\n",
    "B2AKey.gen_and_save(gen_num, 'B2AKey_0')\n",
    "B2AKey.gen_and_save(gen_num, 'B2AKey_1')\n",
    "B2AKey.gen_and_save(gen_num, 'B2AKey_2')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. **Beaver Triples** that will be used for multiplication for two-party arithmetic secret sharing, three-party arithmetic secret sharing, boolean secret sharing and replicated secret sharing.\n",
    "2. Auxiliary parameters required by DICF(Distributed Interval Containment Function) comparison methods: keys used in the DICF method, which includes **DICFKey**, **GrottoDICFKey**, and **SigmaDICFKey**.\n",
    "3. Auxiliary parameters associated with Look Up Table:\n",
    "   * **GeLU Key** is used for the Gaussian Error Linear Unit (GeLU) activation function.\n",
    "   * **Tanh Key** is associated with Tanh activation functions.\n",
    "   * **Reciprocal Sqrt Key** is used for dealing with square root reciprocal and negative index values.\n",
    "   * **Division Key** is used for division operation.\n",
    "4. Auxiliary parameters associated with truncation operation:\n",
    "   * **Wrap Key** is used for truncation operation.\n",
    "   * **RSS Truncation Auxiliary Parameters** is used for truncation of RSS(Replicated Secret Sharing).\n",
    "5. **B2A Key** implements conversion from boolean secret sharing to arithmetic secret sharing.\n",
    "6. Auxiliary parameters associated with comparsion operation:\n",
    "   * **MAC Key** is the corresponding message authentication code key used for comparison operation verification.\n",
    "   * **Malicious CMP Key** implements malicious-secure comparison.\n",
    "7. **VOSKey** is the auxiliary parameters required for the oblivious selection operation.\n",
    "8. **VSigmaKey** is the FSS key class for verifiable sigma."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to these keys, other parameters are also required for some operations, such as the matrix beaver triples for matrix multiplication, which is related to the size of the matrix involved in the operation. Such parameters are usually related to actual operations, so it is hard to generate them in advance, and the generation scheme will be shown in subsequent tutorials."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configuration file\n",
    "Related configuration of the library will read the configuration file ```config.json```, which will be generated under the system path (Linux/Unix: /home/{username}; macOS: /Users/{username}; Windows: C:\\Users\\{username}). \n",
    "Configuration files are used to define the operating parameters of applications. These parameters can include database connection information, network settings, user permissions, and so on. Therefore, users can easily adjust the behavior of the application.\n",
    "Now, let's have an insight into the basic configuration so that you can change the configuration to achieve different operations in the future. See the config section of the [API documentation](https://www.xidiannss.com/doc/NssMPClib/config.html) for more details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## Some utils\n",
    "In ```./NssMPC/common/utils/debug_utils.py```, we provide some tools to help you debug the code. For example, you can use the following code to check the time of a function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from NssMPC.common.utils import get_time\n",
    "res = get_time(func, *args)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_time` will return the result of the function and print the time it takes to run the function. The parameters of `get_time` are the function to be tested and the parameters of the function.\n",
    "\n",
    "In addition, we provide a function for counting the communication costs in secure multiparty computation tasks. You can use the following code to check the communication cost of a function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from NssMPC.common.utils import comm_count\n",
    "res = comm_count(communicator, func, *args)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`comm_count` will return the result of the function and print the communication cost of the function. The parameters of `comm_count` are the parties in `NssMPC/secure_model/mpc_party/semi_honest.py` or Communicator object in `NssMPC/common/network/communicator.py`, the function to be tested and the parameters of the function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "#### Note\n",
    "If you find that some functions mentioned in this tutorial can not run, don't worry. It may be because the auxiliary parameters required for some functions are not generated or the auxiliary parameters are insufficient. You can refer to the tutorial and codes in the```./debug``` package to generate the auxiliary parameters required according to your needs, and distribute the calculation to multiple parties."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
